//
//  SupermarketViewController.m
//  LoveFreshBeen
//
//  Created by 魏根 on 2016/10/15.
//  Copyright © 2016年 魏根. All rights reserved.
//

#import "SupermarketViewController.h"
#import "CyButton.h"
#import "NetWorkingTool.h"
#import "DataModel.h"
#import "categories.h"
#import "CyCategoriesCell.h"
#import "GoodsView.h"
#import "ScanViewController.h"
#import "SearchController.h"


@interface SupermarketViewController () <UITableViewDataSource, UITableViewDelegate>


// 商品分类 tableview
@property (nonatomic, strong) UITableView *TB_Classify;

// 商品视图
@property (nonatomic, strong) GoodsView *CyGoods;

// 数据源
@property (nonatomic, strong) DataModel *dataModel;


@property (nonatomic, weak) CyCategoriesCell *lastCell;

@end

@implementation SupermarketViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setupUI];
    
    
//    [NSNotificationCenter defaultCenter]addObserver:self selector:@selector() name:<#(nullable NSNotificationName)#> object:<#(nullable id)#>
    [SVProgressHUD showWithStatus:@"正在加载数据..."];
    
    [[NetWorkingTool shareInstance] getDatawithParam:@"5" WithUrlString:@"supermarket.json.php" withSuccessBlock:^(NSDictionary *data) {
        
        [self.dataModel yy_modelSetWithDictionary:data[@"data"]];
        
        self.dataModel.products = data[@"data"][@"products"];
        
        [SVProgressHUD dismiss];
        
        [self.TB_Classify reloadData];
        
        [self.TB_Classify selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
        [self tableView:self.TB_Classify didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.TB_Classify.delegate = self;
    
    [self.TB_Classify registerNib:[UINib nibWithNibName:@"CyCategoriesCell" bundle:nil] forCellReuseIdentifier:@"category"];
}


#pragma mark - SETUP UI

- (void)setupUI {
    
    self.navigationController.navigationBar.translucent = NO;
    
    //[UIColor colorWithRed:254/255.0 green:211/255.0 blue:80/255.0 alpha:1]
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:254/255.0 green:211/255.0 blue:65/255.0 alpha:1]];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[self setBtnWithTitle:@"扫一扫" imageName:@"icon_black_scancode" target:self action:@selector(scan_QR_Code)]];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[self setBtnWithTitle:@"搜 索" imageName:@"icon_search" target:self action:@selector(search)]];
    self.navigationItem.titleView = [[NSBundle mainBundle] loadNibNamed:@"TitleView" owner:nil options:nil].lastObject;
    
    [self.view addSubview:self.CyGoods];
    [_CyGoods mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.trailing.bottom.equalTo(self.view);
        make.leading.equalTo(self.TB_Classify.mas_trailing);
    }];
    
    
}


- (CyButton *)setBtnWithTitle:(NSString *)title imageName:(NSString *)imageName target:(id)target action:(SEL)action {
    CyButton *btn = [[CyButton alloc]initWithFrame:CGRectMake(0, 0, 30, 40)];
    
    [btn setImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    [btn setTitle:title forState:UIControlStateNormal];
    
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = [UIFont systemFontOfSize:9];
    
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    return btn;
}





#pragma mark - Search

- (void)search {
    
    SearchController *searchVC = [[SearchController alloc]init];
    
    [self.navigationController pushViewController:searchVC animated:YES];
}


#pragma mark - Scan QR Code


- (void)scan_QR_Code {
    
    ScanViewController* scanVC = [[ScanViewController alloc] init];
    [self.navigationController pushViewController:scanVC animated:YES];
}



#pragma mark - tableview datasource method

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataModel.categories.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    

    CyCategoriesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"category" forIndexPath:indexPath];
    
    categories *obj = self.dataModel.categories[indexPath.row];

    cell.categoryLabel.text = obj.name;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    categories *model = self.dataModel.categories[indexPath.row];
    
    self.CyGoods.data = self.dataModel.products[model.ids];
    self.CyGoods.headerTitle = model.name;
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self.CyGoods.tb_goods reloadData];
    
    
    
}
#pragma mark - lazy load

- (UITableView *)TB_Classify {
    if (!_TB_Classify) {
        
        _TB_Classify = [[UITableView alloc] init];
        
        _TB_Classify.dataSource = self;
        
        [self.view addSubview:_TB_Classify];
        
        _TB_Classify.tableFooterView = [[UIView alloc] init];
        
        _TB_Classify.showsVerticalScrollIndicator = NO;
        _TB_Classify.separatorColor = [UIColor clearColor];

        _TB_Classify.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        
        [_TB_Classify mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.bottom.equalTo(self.view);
            make.top.equalTo(self.view.mas_top);
            make.width.equalTo(@100);
        }];
    }
    return _TB_Classify;
}

- (DataModel *)dataModel {
    if (!_dataModel) {
        _dataModel = [[DataModel alloc] init];
    }

    
    return _dataModel;
}
- (GoodsView *)CyGoods {
    if (!_CyGoods) {
        _CyGoods = [[[NSBundle mainBundle] loadNibNamed:@"GoodsView" owner:nil options:nil] lastObject];
    }
    return _CyGoods;
}


@end
